<template>
  <div class="partner">
    <h3 class="title">合作伙伴管理</h3>
    <el-button
      type="primary"
      @click="addPartnerHandle"
      style="margin-bottom: 20px"
      class="button"
      >添加合作伙伴</el-button
    >
    <el-table :data="partnerData" border style="width: 850px; margin: 0 auto">
      <el-table-column prop="id" label="编号" width="100"> </el-table-column>
      <el-table-column prop="name" label="伙伴名称" width="150">
      </el-table-column>
      <el-table-column label="logo" width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <img :src="scope.row.logo" alt="" class="table-img" />
        </template>
      </el-table-column>
      <el-table-column
        prop="purl"
        label="网址"
        width="300"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="合作伙伴管理"
      :visible.sync="addDialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="partnerAddForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="合作伙伴名称" prop="name">
          <el-input type="text" v-model="partnerAddForm.name"></el-input>
        </el-form-item>
        <el-form-item label="logo" prop="logo">
          <el-button
            class="category"
            type="primary"
            @click="dialogUploadVisible = true"
          >
            图片上传
          </el-button>

          <!-- 上传图片 -->
          <el-dialog
            width="50%"
            title="上传图片"
            :visible.sync="dialogUploadVisible"
            append-to-body
          >
            <el-upload
              class="upload-demo"
              ref="upload"
              action="http://192.168.41.131:1314/upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              :auto-upload="false"
              :on-success="handleSuccess"
            >
              <el-button slot="trigger" size="small" type="primary"
                >选取文件</el-button
              >
              <el-button
                style="margin-left: 10px"
                size="small"
                type="success"
                @click="submitUpload"
                >上传到服务器</el-button
              >
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-dialog>
        </el-form-item>
        <el-form-item label="网址" prop="purl">
          <el-input type="text" v-model="partnerAddForm.purl"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="escHandel">取 消</el-button>
        <el-button type="primary" @click="sureHandel">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "../../api";
export default {
  name: "CooperationAdmain",
  data() {
    return {
      partnerData: [],
      addDialogVisible: false,
      partnerAddForm: {
        name: "",
        logo: "",
        purl: "",
        id: "",
      },

      //上传图片
      dialogUploadVisible: false,
      fileList: [],

      //判断谁引起的对话框
      type: "",
      //旗帜，表示是否重新渲染数据
      flag: true,
    };
  },
  mounted() {
    //获取数据
    if (this.flag) {
      this.http();
    }
  },
  watch: {
    flag: function (newVal, oldVal) {
      // console.log(newVal);
      if (newVal) {
        //true 则统一渲染数据
        this.http();
      }
    },
  },
  methods: {
    //添加事件
    addPartnerHandle() {
      this.addDialogVisible = true;
      this.type = "add";
    },
    handleClick() {},
    handleClose() {
      this.addDialogVisible = false;
    },
    //编辑
    handleEdit(index, row) {
      this.addDialogVisible = true;
      this.type = "edit";
      console.log(row);
      //提供数据
      this.partnerAddForm.name = row.name;
      this.partnerAddForm.purl = row.purl;
      this.partnerAddForm.id = row.id;
    },
    //删除
    handleDelete(index, row) {
      let id = row.id;
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.deletePartner({ id }).then((res) => {
            // console.log(res);
            if (res.status == 200) {
              //重新渲染页面
              this.flag = true;

              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //取消事件
    escHandel() {
      // 将类型清空
      this.type = "";
      this.addDialogVisible = false;
    },

    //添加，编辑 确定事件
    sureHandel() {
      console.log(this.partnerAddForm);
      console.log(this.type);

      //获取属性
      let name = this.partnerAddForm.name;
      let logo = "";
      let purl = this.partnerAddForm.purl;
      let id = this.partnerAddForm.id;
      // 发送数据请求
      if (this.type == "add") {
        //添加数据
        api.addPartner({ name, logo, purl }).then((res) => {
          // console.log(res);
          if (res.status == 200) {
            this.$message({
              message: "添加成功",
              type: "success",
            });

            //重新渲染页面
            this.flag = true;
          }
        });
      } else if (this.type == "edit") {
        //编辑数据
        console.log({ name, logo, purl, id });
        api.updatePartner({ name, logo, purl, id }).then((res) => {
          // console.log(res);
          if (res.status == 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });

            //重新渲染页面
            this.flag = true;
          }
        });
      }
      this.addDialogVisible = false;
    },

    //上传图片的事件
    handlePreview(file) {
      console.log(file);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleSuccess(response, file, fileList) {
      console.log("response, file, fileList", response, file, fileList);
    },

    //封装 查询数据
    http() {
      //关闭flag
      this.flag = false;
      api.selectPartner().then((res) => {
        if (res.status === 200) {
          this.partnerData = res.data;
        }
        // console.log(res);
      });
    },
  },
};
</script>

<style scoped lang='less'>
.partner {
  margin-left: 20px;

  .title {
    text-align: center;
    padding: 20px;
    background-color: #c4b4a9;
    margin-bottom: 10px;
  }

  .button {
    margin-top: 10px;
    margin-left: 20px;
  }
}
.table-img {
  width: 100px;
  height: 50px;
}
</style>